package com.yangyang.androidconnect;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Environment;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.yangyang.androidconnect.config.ProjectConfig;
import com.yangyang.androidconnect.service.FileIndexService;
import com.yangyang.androidconnect.websocket.MyWebSocketServer;

public class MyForegroundService extends Service {
    private ServerManager serverManager = null;
    private MyWebSocketServer wsServer = null;

    private static Context appContext;

    public static Context getAppContext() {
        return appContext;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        appContext = getApplicationContext();
        try {
            wsServer = new MyWebSocketServer(ProjectConfig.WEB_SOCKET_PORT);
            wsServer.start();
            //
            serverManager = new ServerManager(this, ProjectConfig.SERVER_PORT);
            serverManager.startServer();

            // 启动文件索引服务
            startFileIndexing();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    private void startFileIndexing() {
        FileIndexService fileIndexService = new FileIndexService(this);
        //fileIndexService.setIndexingListener(this);
        String rootPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        fileIndexService.startIndexing(rootPath);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 在此处添加您的服务逻辑，例如执行一些后台任务
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // 短时间显示
            //Toast.makeText(this, "后台任务", Toast.LENGTH_SHORT).show();
            startForeground(1, getForegroundNotification());
        }
        // 为了保持服务一直运行，返回 START_STICKY
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (serverManager != null) serverManager.stopServer();
        try {
            if (wsServer != null) wsServer.stop();
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
        // 服务销毁时执行的操作
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private Notification getForegroundNotification() {
        String NOTIFICATION_CHANNEL_ID = "foreground_service_channel";
        String channelName = "Foreground Service";

        NotificationChannel channel = new NotificationChannel(
                NOTIFICATION_CHANNEL_ID,
                channelName,
                NotificationManager.IMPORTANCE_LOW);

        NotificationManager manager = getSystemService(NotificationManager.class);
        manager.createNotificationChannel(channel);

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);

        return new Notification.Builder(this, NOTIFICATION_CHANNEL_ID)
                .setContentTitle("服务运行中")
                .setContentText("正在后台保持连接")
                .setSmallIcon(R.drawable.ic_notification) // 使用我们创建的图标
                .setContentIntent(pendingIntent)
                .setOngoing(true) // 添加持续通知标志
                .build();
    }
}
